package pipe;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class Producer implements Runnable {
    private Pipe pipe;
    private int interval =2000;

    public Producer(Pipe pipe, int interval) {
        this.pipe = pipe;
        this.interval = interval;
    }

    @Override
    public void run() {
        Pipe.SinkChannel sink = pipe.sink();
        while (true){
            String k = "msg_from_"+Thread.currentThread().getName()+System.currentTimeMillis();
            ByteBuffer buffer = ByteBuffer.wrap(k.getBytes());
            while (buffer.hasRemaining()){
                System.out.println(Thread.currentThread().getName()+" : "+k);
                try {
                    sink.write(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(this.interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }
}
